<?php 
namespace common\service;

use think\Log;
use common\model\sell66\BotInv;
use common\libs\StringHelper;

class BotInvService
{
	
	public static $instance = null;

	public static function getInstance()
	{
		if (self::$instance == null) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	public function lists($userId, $appId = 730, $page = 1, $limit = 25)
	{
		$count = BotInv::getBotINVNum($userId, $appId);

		$botInv = BotInv::getBotINV($userId, $appId, $page, $limit);
		if ($botInv) {
			foreach ($botInv as &$inv) {
				$expireTime = $inv['cd_time'] - time();
				$leftTime = StringHelper::formatLeftTime($expireTime);

				$inv = array(
					'bot_inv_id' => $inv['bot_inv_id'],
					'app_id' => $inv['app_id'],
					'market_name' => $inv['market_name'],
					'market_hash_name' => $inv['market_hash_name'],
					'cd_time' => $leftTime,
					'cd_status' => $inv['cd_time'] > 0 ? true : false,
					'status' => $inv['status'],
					'status_name' => botinv::$statusNote[$inv['status']],
					'selected' => false,
					'image' => StringHelper::getSteamImageUrl($inv['app_id'], $inv['market_hash_name']),
				);
			}
		}

		return array(
			'games' => InvService::getInstance()->getAppIds($appId),
			'list' => $botInv,
			'totalCount' => $count,
			'totalPage' => ceil($count / $limit),
			'page' => $page,
		);
	}

	public function checkUserInvs($userId, $appId, array $botInvIds)
	{
		if (empty($botInvIds)) {
			return [];
		}

		$count = BotInv::checkUserInvs($userId, $appId, $botInvIds);
		if ($count === count($botInvIds)) {
			return true;
		}
		
		return false;
	}

	public function checkWalletInvs($userId, $appId, array $botInvIds)
	{
		if (empty($botInvIds)) {
			return [];
		}

		$count = BotInv::checkWalletInvs($userId, $appId, $botInvIds);
		if ($count === count($botInvIds)) {
			return true;
		}
		
		return false;
	}

	public function checkWaitTakingInvs($userId, array $botInvIds)
	{
		if (empty($botInvIds)) {
			return false;
		}

		$count = BotInv::checkWaitTakingInvs($userId, $botInvIds);
		if ($count === count($botInvIds)) {
			return true;
		}
		
		return false;
	}

	public function getBotInvByUserId($userId, $appId, array $botInvIds)
	{
		return BotInv::getBotInvByUserId($userId, $appId, $botInvIds);
	}

	public function checkTakeBackBotInvsByIds($userId, $botInvIds)
    {
        $botInvList = BotInv::getBotInvsByIds($botInvIds);
        if(empty($botInvList)){
            return -1;
        }
        foreach($botInvList as $botInvInfo){
            if($userId != $botInvInfo['user_id']){
                return -2;
            }
            if($botInvInfo['status'] != BotInv::$statusMaps['issued'] ||
                ($botInvInfo['take_status'] != BotInv::$takeStatusMaps['init'] && $botInvInfo['take_status'] != BotInv::$takeStatusMaps['cancel'])){
                return -3;
            }
        }
        return $botInvList;
    }

    /**
     * 绑定用户物品
     */
    public function bindUserInv($userId, $botInvList)
    {
        if(empty($botInvList)){
            return true;
        }
        $botInvId = array_column($botInvList, 'bot_inv_id');
        $update = [
            'user_id' => $userId,
        ];
        return BotInv::updateByBotInvIds($botInvId, $update);
    }

    /**
     * 解绑用户物品
     */
    public function UntieUserInv($userId, $botInvList)
    {
        if(empty($botInvList)){
            return true;
        }
        $botInvId = array_column($botInvList, 'bot_inv_id');
        $update = [
            'user_id' => 0,
        ];
        return BotInv::updateByBotInvIds($botInvId, $update);
    }

    /**
     * 解绑用户所有物品
     */
    public function UntieAllUserInv($userId)
    {
        $condition = [
            'user_id' => $userId,
        ];
        $update = [
            'user_id' => 0,
        ];
        return BotInv::updateBotInv($condition, $update);
    }
}